package org.balthie.demo.opensource.org.codehaus.plexus.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class MatchPattern
{
    private final String source;
    
    final String regexPattern;
    
    private final String separator;
    
    private final String[] tokenized;
    
    private final char[][] tokenizedChar;
    
    private MatchPattern(String source, String separator)
    {
        this.regexPattern = (SelectorUtils.isRegexPrefixedPattern(source) ? source.substring("%regex[".length(), source
                .length()
                - "]".length()) : null);
        this.source = (SelectorUtils.isAntPrefixedPattern(source) ? source.substring("%ant[".length(), source
                .length()
                - "]".length()) : source);
        this.separator = separator;
        this.tokenized = tokenizePathToString(this.source, separator);
        this.tokenizedChar = new char[this.tokenized.length][];
        for(int i = 0; i < this.tokenized.length; i++)
        {
            this.tokenizedChar[i] = this.tokenized[i].toCharArray();
        }
    }
    
    /**
     * @author：balthie@126.com
     * @createtime ： 2022年10月31日 下午3:40:51
     * @description TODO 一句话描述
     * @since version 初始于版本 TODO
     * @param str
     * @param isCaseSensitive
     * @return
     */
    public boolean matchPath(String str, boolean isCaseSensitive)
    {
        System.out.println(String.format("matchPath [%s] match [%s]  isCaseSensitive[%s]", str, this.regexPattern, isCaseSensitive));
        
        if(this.regexPattern != null)
        {
            return str.matches(this.regexPattern);
        }
        
        return SelectorUtils.matchAntPathPattern(this, str, this.separator, isCaseSensitive);
    }
    
    @SuppressWarnings("unchecked")
    boolean matchPath(String str, char[][] strDirs, boolean isCaseSensitive)
    {
        List strDirsArray = new ArrayList();
        for(char subdir[] : Arrays.asList(strDirs))
        {
            strDirsArray.add(String.valueOf(subdir));
        }
        
        if(this.regexPattern != null)
        {
            System.out.println(String.format("matchPath [%s] match [%s] isCaseSensitive[%s] strDirs[%s] matched[%s]", str, this.regexPattern, isCaseSensitive, strDirsArray, str.matches(this.regexPattern)));
            return str.matches(this.regexPattern);
        }
        
        return SelectorUtils.matchAntPathPattern(getTokenizedPathChars(), strDirs, isCaseSensitive);
    }
    
    public boolean matchPatternStart(String str, boolean isCaseSensitive)
    {
        if(this.regexPattern != null)
        {
            return true;
        }
        
        String altStr = str.replace('\\', '/');
        
        return (SelectorUtils.matchAntPathPatternStart(this, str, File.separator, isCaseSensitive))
                ||
                (SelectorUtils.matchAntPathPatternStart(this, altStr, "/", isCaseSensitive));
    }
    
    public String[] getTokenizedPathString()
    {
        return this.tokenized;
    }
    
    public char[][] getTokenizedPathChars()
    {
        return this.tokenizedChar;
    }
    
    public boolean startsWith(String string)
    {
        return this.source.startsWith(string);
    }
    
    static String[] tokenizePathToString(String path, String separator)
    {
        List ret = new ArrayList();
        StringTokenizer st = new StringTokenizer(path, separator);
        while (st.hasMoreTokens())
        {
            ret.add(st.nextToken());
        }
        return (String[]) ret.toArray(new String[0]);
    }
    
    static char[][] tokenizePathToCharArray(String path, String separator)
    {
        String[] tokenizedName = tokenizePathToString(path, separator);
        char[][] tokenizedNameChar = new char[tokenizedName.length][];
        for(int i = 0; i < tokenizedName.length; i++)
        {
            tokenizedNameChar[i] = tokenizedName[i].toCharArray();
        }
        return tokenizedNameChar;
    }
    
    public static MatchPattern fromString(String source)
    {
        return new MatchPattern(source, File.separator);
    }
}